package com.example.zyfypt_206shj.fragement;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.zyfypt_206shj.R;
import com.example.zyfypt_206shj.adapter.TcaseAdapter;
import com.example.zyfypt_206shj.bean.SessionID;
import com.example.zyfypt_206shj.bean.TcaseBean;
import com.example.zyfypt_206shj.bean.UserInfo;
import com.example.zyfypt_206shj.iface.RecyclerViewUtils;
import com.example.zyfypt_206shj.iface.TcaseListener;
import com.example.zyfypt_206shj.model.TcaseModel;

import java.util.List;


/**
 * Created by Neusoft on 2017/4/9.
 */
//implements RecyclerViewUtils.LoadingData

public class Fragment_tcase extends Fragment {
    private View view=null;
    private Context context;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private TcaseAdapter tcaseAdapter;
    private List<TcaseBean> list=null;
    private boolean flag = true;
    private TcaseListener tcaseListener = new TcaseListener() {
        @Override
        public void onResponse(List<TcaseBean> beanlist) {
            if(flag){
                list = beanlist;
                tcaseAdapter.setList(list);
                tcaseAdapter.notifyDataSetChanged();
                flag = false;
            }else{
                list.addAll(beanlist);
                tcaseAdapter.setList(list);
                tcaseAdapter.notifyDataSetChanged();
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail" + msg, Toast.LENGTH_SHORT).show();
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment的布局文件
        view=inflater.inflate(R.layout.fragment_tcase,container,false);
        //获取布局上的控件，注意通过view获取
        initRecyclerView();
//        RecyclerViewUtils.OnScrollListener(getActivity(), recyclerView);
        TcaseModel tcaseModel = new TcaseModel();
        tcaseModel.getResultList("tcase", 1, SessionID.sessionID, tcaseListener);

        //返回动态生成的view
        return view;
    }

    private void initRecyclerView() {
        layoutManager = new LinearLayoutManager(getContext());

        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView_tcase);
        //创建默认线性布局
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item的高度，可以提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        //list=getData();
        //实例化adapter
        tcaseAdapter=new TcaseAdapter(getContext());
        tcaseAdapter.setList(list);
        recyclerView.setAdapter(tcaseAdapter);

    }

//    @Override
//    public void onLoadMore() {
//        TcaseModel tcaseModel = new TcaseModel();
//        tcaseModel.getResultList("tcase", 2, "rv1l2tidbomann2qn86nln6ur2", tcaseListener);
//    }

}
